package com.xbx.zuul.controller.login;

import com.xbx.baseweb.controller.ResultUtils;
import com.xbx.baseweb.model.ResponseModel;
import com.xbx.baseweb.model.User;
import com.xbx.common.utils.EntityUtils;
import com.xbx.zuul.entity.vo.UserVo;
import com.xbx.zuul.utils.UserUtlis;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author 89005691
 * @create 2018-09-12 13:53
 */
@Api(tags = "登陆管理")
@Controller
@RequestMapping("login")
public class LoginController {

    @Value("${logoutUrl}")
    private String logoutUrl;

    @Value("${loginUrl}")
    private String loginUrl;

    @ApiOperation(value = "退出登录", httpMethod = "POST")
    @RequestMapping(value = "logout", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ResponseModel logout() {

        UserUtlis.logout();

        return ResultUtils.success("object", logoutUrl);
    }

    @ApiOperation(value = "检查是否已经登陆", httpMethod = "POST")
    @RequestMapping(value = "checkLogin", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ResponseModel checkLogin() {

        User user = UserUtlis.getLoginUserInfo();

        if (user == null) {

            return ResultUtils.fail(-1000, loginUrl);
        } else {

            return ResultUtils.success("object", EntityUtils.transform(user, UserVo.class));
        }
    }
}
